// src/worker.js

import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader.js"

// 监听主线程发来的消息
self.onmessage = function (event) {
  const { type, files } = event.data

  if (type === "loadGLTF") {
    const result = []
    let loaded = 0

    files.forEach(({ name, type, file }) => {
      const reader = new FileReader()
      reader.onload = function (e) {
        result.push({
          name,
          type,
          data: e.target.result,
        })
        loaded++
        if (loaded === files.length) {
          // 全部读取完毕
          self.postMessage({
            type: "loaded",
            files: result,
          })
        }
      }
      // gltf用文本，其它用ArrayBuffer
      if (name.endsWith(".gltf")) {
        reader.readAsText(file)
      } else {
        reader.readAsArrayBuffer(file)
      }
    })
  }
}
